package net.huashitong.zhApp.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.MessageList;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.Project.filter.MessageListFilter;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.oversee.entity.SupervisionLog;
import net.huashitong.oversee.entity.SupervisionTask;
import net.huashitong.oversee.entity.TaskMethon;
import net.huashitong.oversee.entity.TaskRecord;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.oversee.service.SupervisionLogService;
import net.huashitong.oversee.service.SupervisionTaskService;
import net.huashitong.oversee.service.TaskMethonService;
import net.huashitong.oversee.service.TaskRecordService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/zhApp/taskToTack")
public class AppTaskToTackController extends BaseCtrl {

    @Autowired
    private SupervisionTaskService supervisionTaskService;
    @Autowired
    private SupervisionLogService supervisionLogService;
    @Autowired
    private TaskMethonService menthonService;
    @Autowired
    private TaskRecordService taskRecordService;
    @Autowired
    private NewProjectsService newProjectsService;


    @ResponseBody
    @RequestMapping(value = "/lists", method = RequestMethod.POST)
    public Map<String, Object> warningList(@RequestBody SupervisionTaskFilter filter) {

        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (StringUtils.isBlank(filter.getTackFlag())) {
            filter.setTackFlag("1");
        }
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", "202");
            result.put("message", "无用户权限");
            return result;
        }else{
            filter.setUserinfo(user.getPhone());
            filter.setBranchType(user.getBranchType());
            filter.setRole(user.getRole());
            Map<String, Object> result = new HashMap<>();
            List<SupervisionTask> resList = supervisionTaskService.getTaskLists(filter);
            if(ObjectUtils.isBlank(resList)){
                result.put("code", "200");
                result.put("message", "成功获取数据");
                result.put("userAuth", user);
                result.put("data", new ArrayList<>());
                result.put("count", 0);
                return result;
            }
            for (SupervisionTask task : resList) {
                NewProjects projects = newProjectsService.getById(task.getProjectId());
                task.setLeaderName(projects.getResUser());
                task.setLeaderPhone(projects.getResPhone());
            }

            result.put("code", "200");
            result.put("message", "成功获取数据");
            result.put("userAuth", user);
            result.put("data", resList);
            result.put("count", filter.getPaging().getRecords());
            return result;
        }
    }


    /**
     * 催办记录
     * @param id
     * @param userPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/supervisionAskList/{id}/{userPhone}", method = RequestMethod.GET)
    public Map<String, Object> showSupervisionAskList(@PathVariable String id, @PathVariable String userPhone) {
        BnpUser user = queryUserAuth(userPhone);
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", "202");
            result.put("message", "无用户权限");
            return result;
        }else{
            SupervisionTask task = supervisionTaskService.getById(id);
            List<SupervisionLog> logs = supervisionLogService.getMissionLog(id);
            for (SupervisionLog items : logs) {
                TaskMethon methon = menthonService.getMethon(items.getMethonCode());
                if (ObjectUtils.isNotBlank(methon)) {
                    items.setMethonName(methon.getMethonName());
                }
                if (StringUtils.isBlank(items.getDatatime())) {
                    items.setDatatime(items.getCreateTime().replace("-", ""));
                }
            }
            Map<String, Object> result = new HashMap<>();
            result.put("code", "200");
            result.put("message", "成功获取数据");
            result.put("userAuth", user);
            result.put("task", task);
            result.put("data", logs);
            return result;
        }
    }


    /**
     * 反馈情况
     * @param id
     * @param userPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/feedbackInfoList/{id}/{userPhone}", method = RequestMethod.GET)
    public Map<String, Object> showfeedbackInfoList(@PathVariable String id, @PathVariable String userPhone) {
        BnpUser user = queryUserAuth(userPhone);
        if (ObjectUtils.isBlank(user)) {
            Map<String, Object> result = new HashMap<>();
            result.put("code", "202");
            result.put("message", "无用户权限");
            return result;
        }else{
            SupervisionTask task = supervisionTaskService.getById(id);
            //被督查单位的<反馈记录>
            List<TaskRecord> lists = taskRecordService.getRecordsById(id);
            for (TaskRecord record : lists) {
                if (StringUtils.isBlank(record.getDatatime())) {
                    record.setDatatime(record.getCreateTime().replace("-", ""));
                }
            }
            Map<String, Object> result = new HashMap<>();
            result.put("code", "200");
            result.put("message", "成功获取数据");
            result.put("userAuth", user);
            result.put("task", task);
            result.put("data", lists);
            return result;
        }
    }
}
